package com.ruoyi.project.jxy.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.jxy.domain.Corpus;
import com.ruoyi.project.jxy.domain.Entity;
import com.ruoyi.project.jxy.mapper.CorpusMapper;
import com.ruoyi.project.jxy.mapper.EntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.jxy.mapper.SlotEntityRefMapper;
import com.ruoyi.project.jxy.domain.SlotEntityRef;
import com.ruoyi.project.jxy.service.ISlotEntityRefService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@Service
@Transactional
public class SlotEntityRefServiceImpl extends ServiceImpl<SlotEntityRefMapper, SlotEntityRef> implements ISlotEntityRefService {
    @Resource
    private EntityMapper entityMapper;
    @Resource
    private CorpusMapper corpusMapper;

    @Override
    public List<Map> getSlotEntityList(Map map) {
        return baseMapper.getSlotEntityList(map);
    }

    @Override
    public int updateSlotEntityInfo(SlotEntityRef newSlotEntityRef) {
        //更新对应关系
        Entity newEntity = entityMapper.selectById(newSlotEntityRef.getEntityId());
        if (newEntity == null) {
            throw new RuntimeException("实体/辅助词id 不存在");
        } else {
            newSlotEntityRef.setType(newEntity.getType());
        }
        SlotEntityRef temp = baseMapper.selectById(newSlotEntityRef.getId());
        //意图id
        long intentionId = temp.getIntentionId();
        List<Map> lst = baseMapper.getSlotCodeByIntentionId(intentionId);
        //槽位实体对应关系map
        Map<String, String> slotCodeMap = new HashMap();
        if (lst != null && lst.size() > 0) {
            for (Map map :
                    lst) {
                slotCodeMap.put(map.get("slot").toString(), map.get("code").toString());
            }
        }
        slotCodeMap.put(temp.getSlot(), newEntity.getCode());
        //从语料表中查询出该意图下所有的相关语料,并更新其对应关系
        QueryWrapper corpusWrapper = new QueryWrapper();
        corpusWrapper.eq("intention_id", intentionId);
        List<Corpus> corpuses = corpusMapper.selectList(corpusWrapper);
        for (int i = 0; i < corpuses.size(); i++) {
            //内容
            Corpus corpus = corpuses.get(i);
            String content = corpus.getContent();
            Pattern p = Pattern.compile("\\{([^}]*)\\}");
            Matcher m = p.matcher(content);
            while (m.find()) {
                //槽位
                String slot = m.group(1);
                String code = slotCodeMap.get(slot);
                if(StringUtils.isNotBlank(code)){
                    content = content.replaceAll("\\{" + slot + "}", "{" + code + "}");
                }
            }
            corpus.setSolts(content);
            corpusMapper.updateById(corpus);
        }
        //更新该用户所有实体辅助词引用次数
        Entity entity = new Entity();
        entity.setCreateUser(SecurityUtils.getUsername());
        entityMapper.updateRefTimes(entity);
        //更新对应关系
        baseMapper.updateById(newSlotEntityRef);
        return 1;
    }
}
